package io.gitee.caoxiaoyu97.log.dynamic.level.logback;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.turbo.TurboFilter;
import ch.qos.logback.core.spi.FilterReply;
import io.gitee.caoxiaoyu97.log.dynamic.level.LogDynamicLevelConstant;
import org.slf4j.MDC;
import org.slf4j.Marker;

/*********************************************************
 * 文件名称：LogbackLogDynamicLevelMatchFilter.java
 * 系统名称：bfts
 * 模块名称：com.hundsun.tbsp.bfts.other.base.log.logback
 * 功能说明：
 * 开发人员 @author：caoxy31978
 * 开发时间 @date：2025/9/24 10:55
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class LogbackLogDynamicLevelMatchFilter extends TurboFilter {
    private String key = LogDynamicLevelConstant.MDC_LEVEL_KEY;

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
        String mdcLevel = MDC.get(key);
        if (mdcLevel != null){
            Level checkLevel = Level.toLevel(mdcLevel,null);
            if (checkLevel != null){
                return level.isGreaterOrEqual(checkLevel) ? FilterReply.ACCEPT : FilterReply.DENY;
            }
        }
        return FilterReply.NEUTRAL;
    }
}
